package com.qianbao.qianbaobusiness.ui.cashier;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baseandroid.config.Constant;
import com.baseandroid.mvp.BaseMvpActivity;
import com.baseandroid.utils.FormatUtil;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.CashierDetailBean;
import com.qianbao.qianbaobusiness.json.DataListBean;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 交易详情
 */
public class CashierDetailActivity extends BaseMvpActivity<CashierDetailView, CashierDetailPresenter>
        implements CashierDetailView{

    /**
     * 控件相关
     */
    @BindView(R.id.toolbar_back)
    RelativeLayout toolbarBack;     //返回
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;      //标题
    @BindView(R.id.right_text)
    TextView rightText;     //右侧文本（后续退款操作需要）
    @BindView(R.id.iv)
    ImageView iv;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_success_state)
    TextView tvSuccessState;
    @BindView(R.id.tv_state)
    TextView tvState;
    @BindView(R.id.tv_state_sub)
    TextView tvStateSub;
    @BindView(R.id.tv_cost_amount)      //交易金额
    TextView tvCostAmount;
    @BindView(R.id.ll_cost_amount)
    LinearLayout llCostAmount;
    @BindView(R.id.tv_preferentialless_amount)
    TextView tvPreferentiallessAmount;
    @BindView(R.id.ll_preferentialless_amount)
    LinearLayout llPreferentiallessAmount;
    @BindView(R.id.tv_amount_preferential)
    TextView tvAmountPreferential;
    @BindView(R.id.ll_amount_preferential)
    LinearLayout llAmountPreferential;
    @BindView(R.id.tv_amount_pay)
    TextView tvAmountPay;
    @BindView(R.id.ll_amount_pay)
    LinearLayout llAmountPay;
    @BindView(R.id.tv_pay_type)
    TextView tvPayType;
    @BindView(R.id.ll_pay_type)
    LinearLayout llPayType;
    @BindView(R.id.tv_pay_time)
    TextView tvPayTime;         //交易日期
    @BindView(R.id.ll_pay_time)
    LinearLayout llPayTime;
    @BindView(R.id.tv_merchant_name)
    TextView tvMerchantName;        //商户名称
    @BindView(R.id.ll_merchant_name)
    LinearLayout llMerchantName;
    @BindView(R.id.tv_card_no)
    TextView tvCardNo;          //银行卡号
    @BindView(R.id.ll_card_no)
    LinearLayout llCardNo;
    @BindView(R.id.tv_order_no)
    TextView tvOrderNo;         //订单编号
    @BindView(R.id.ll_order_no)
    LinearLayout llOrderNo;
    @BindView(R.id.tv_pay_no)
    TextView tvPayNo;
    @BindView(R.id.ll_pay_no)
    LinearLayout llPayNo;
    @BindView(R.id.tv_note)
    TextView tvNote;
    @BindView(R.id.ll_note)
    LinearLayout llNote;

    /**
     * 变量相关
     */
    private DataListBean bean ;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_cashier_detail;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void setupView() {
        bean = (DataListBean) getIntent().getSerializableExtra("bean");
        toolbarTitle.setText(getApplicationContext().getResources().getString(R.string.cashier_detail));
    }

    @Override
    protected void setupData(Bundle savedInstanceState) {
        getPresenter().getOrderInfo(bean.getMerchantNo(),bean.getOrderNo());
    }

    @OnClick({R.id.toolbar_back})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.toolbar_back :    //返回
                    finish();
                break;
        }
    }

    @Override
    public void onComplete() {

    }

    @Override
    public void onNext(CashierDetailBean cashierDetailBeanData) {
        tvCostAmount.setText(cashierDetailBeanData.getOrderAmt());       //消费金额
        tvPayTime.setText(cashierDetailBeanData.getTradeDate());         //消费日期
        tvOrderNo.setText(cashierDetailBeanData.getOrderNo());           //订单编号
        String orderState = "";
        switch (cashierDetailBeanData.getTradeStatus()){
            case Constant.TRADE_SUCCESS :   //交易成功
                orderState = getApplicationContext().getResources()
                        .getString(R.string.trade_success);
                break;

            case Constant.TRADE_FAIL :   //交易失败
                orderState = getApplicationContext().getResources()
                        .getString(R.string.trade_fail);
                break;

            case Constant.TRADE_UNDO :   //交易撤销
                orderState = getApplicationContext().getResources()
                        .getString(R.string.trade_undo);
                break;

            case Constant.TRADE_DEALING :   //交易处理中
                orderState = getApplicationContext().getResources()
                        .getString(R.string.trade_dealing);
                break;

            case Constant.TRADE_IMPACT :   //交易已冲正
                orderState = getApplicationContext().getResources()
                        .getString(R.string.trade_impact);
                break;

            case Constant.TRADE_IMPACT_FAIL :   //交易冲正失败
                orderState = getApplicationContext().getResources()
                        .getString(R.string.trade_impact_fail);
                break;

            case Constant.TRADE_UNDO_SUCCESS :   //交易撤销成功
                orderState = getApplicationContext().getResources()
                        .getString(R.string.trade_undo_success);
                break;

            case Constant.TRADE_UNDO_FAIL :   //交易撤销失败
                orderState = getApplicationContext().getResources()
                        .getString(R.string.trade_undo_fail);
                break;
        }

        llMerchantName.setVisibility(View.VISIBLE);
        tvMerchantName.setText(cashierDetailBeanData.getMerchantName());         //商户名称
        llCardNo.setVisibility(View.GONE);                      //卡号
        llPayNo.setVisibility(View.GONE);               //支付号(默认不显示)
        llNote.setVisibility(View.GONE);
        boolean noReturnAtBool = 0.00 < Double.parseDouble(cashierDetailBeanData.getNoReturnAmt());
        boolean discountAtBool = 0.00 < Double.parseDouble(cashierDetailBeanData.getDiscountAmt());
        llAmountPreferential.setVisibility(View.GONE);
        llPreferentiallessAmount.setVisibility(View.GONE);


        switch (cashierDetailBeanData.getOrderType()){
            case Constant.ORDER_TYPE_CARD :
                iv.setImageResource(R.drawable.icon_transaction_detail_success);
                tvMoney.setVisibility(View.VISIBLE);
                if (cashierDetailBeanData.getTradeAmt().startsWith("-") || cashierDetailBeanData.getTradeAmt().startsWith("+")) {
                    tvMoney.setText(cashierDetailBeanData.getTradeAmt());
                } else {
                    tvMoney.setText("+" + cashierDetailBeanData.getTradeAmt());      //总金额
                }
                llAmountPay.setVisibility(View.GONE);
                tvSuccessState.setVisibility(View.VISIBLE);
                tvSuccessState.setText(orderState);
                tvState.setVisibility(View.GONE);
                tvStateSub.setVisibility(View.GONE);
                if (discountAtBool) {
                    llAmountPreferential.setVisibility(View.VISIBLE);
                    tvAmountPreferential.setText("-" + cashierDetailBeanData.getDiscountAmt());//优惠金额
                } else {
                    llAmountPreferential.setVisibility(View.GONE);
                }
                if (noReturnAtBool) {
                    llPreferentiallessAmount.setVisibility(View.VISIBLE);
                    tvPreferentiallessAmount.setText(cashierDetailBeanData.getNoReturnAmt());//不参与优惠金额
                } else {
                    llPreferentiallessAmount.setVisibility(View.GONE);
                }

                break;
            case Constant.ORDER_TYPE_ZHIFUBAO : //orderType为2的时候显示
                llPayNo.setVisibility(View.VISIBLE);
                tvPayNo.setText(cashierDetailBeanData.getPayNo());           //支付号
                llNote.setVisibility(View.VISIBLE);
                tvNote.setText(cashierDetailBeanData.getRemark());
                switch (cashierDetailBeanData.getPayType()){
                    case Constant.ORDER_TYPE_ZHIFUBAO:
                        payTwoOrThree(orderState,cashierDetailBeanData);
                        break;

                    case Constant.ORDER_TYPE_WEIXIN :
                        payTwoOrThree(orderState,cashierDetailBeanData);
                        break;

                    default:
                        payOther(orderState,cashierDetailBeanData);
                        break;
                }
                break;

            case Constant.ORDER_TYPE_WEIXIN :
                iv.setVisibility(View.GONE);
                tvMoney.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(cashierDetailBeanData.getTradeAmt())) {
                    llAmountPay.setVisibility(View.VISIBLE);
                    tvAmountPay.setText(cashierDetailBeanData.getTradeAmt());
                } else {
                    llAmountPay.setVisibility(View.GONE);
                }
                tvSuccessState.setVisibility(View.GONE);
                tvState.setVisibility(View.GONE);
                tvStateSub.setVisibility(View.GONE);
                break;

            default:
                iv.setVisibility(View.GONE);
                tvMoney.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(cashierDetailBeanData.getTradeAmt())) {
                    llAmountPay.setVisibility(View.VISIBLE);
                    tvAmountPay.setText(cashierDetailBeanData.getTradeAmt());
                } else {
                    llAmountPay.setVisibility(View.GONE);
                }
                tvSuccessState.setVisibility(View.GONE);
                tvState.setVisibility(View.GONE);
                tvStateSub.setVisibility(View.GONE);
                break;
        }

        switch (cashierDetailBeanData.getPayType()){    //支付方式
            case Constant.ORDER_TYPE_CARD :     //刷卡
                    tvPayType.setText(getApplicationContext()
                            .getResources().getString(R.string.pay_to_card));
                    llMerchantName.setVisibility(View.GONE);
                llCardNo.setVisibility(View.VISIBLE);
                tvCardNo.setText(FormatUtil.changeBankCard(cashierDetailBeanData.getCardNo()));//卡号
                break;

            case Constant.ORDER_TYPE_ZHIFUBAO :     //支付宝
                llPayNo.setVisibility(View.VISIBLE);
                tvPayNo.setText(cashierDetailBeanData.getPayNo());           //支付号
                tvPayType.setText(getApplicationContext()
                        .getResources().getString(R.string.pay_to_alipay));

                break;

            case Constant.ORDER_TYPE_WEIXIN :     //微信
                llPayNo.setVisibility(View.VISIBLE);
                tvPayNo.setText(cashierDetailBeanData.getPayNo());           //支付号
                tvPayType.setText(getApplicationContext()
                        .getResources().getString(R.string.pay_to_wechat));
                break;

            case Constant.ORDER_TYPE_QIANBAO :     //其他（选择支付渠道）
                switch (cashierDetailBeanData.getPayWay()){
                    case Constant.PAY_WAY_ALI :     //支付宝
                        tvPayType.setText(getApplicationContext()
                                .getResources().getString(R.string.pay_to_alipay));
                        break;

                    case Constant.PAY_WAY_WECHAT :     //微信
                        tvPayType.setText(getApplicationContext()
                                .getResources().getString(R.string.pay_to_wechat));
                        break;

                    case Constant.PAY_WAY_BHH :     //支付宝
                        tvPayType.setText(getApplicationContext()
                                .getResources().getString(R.string.pay_to_bhh));
                        break;

                    case Constant.PAY_WAY_BANK :     //支付宝
                        tvPayType.setText(getApplicationContext()
                                .getResources().getString(R.string.pay_to_bank));
                        break;
                }
                break;
        }
    }

    private void payOther(String orderState, CashierDetailBean bean) {
        switch (bean.getTradeStatus()){
            case Constant.TRADE_SUCCESS :
                iv.setImageResource(R.drawable.icon_transaction_detail_success);
                tvMoney.setVisibility(View.VISIBLE);
                llAmountPay.setVisibility(View.GONE);
                if (bean.getTradeAmt().startsWith("-") || bean.getTradeAmt().startsWith("+")) {
                    tvMoney.setText(bean.getTradeAmt());
                } else {
                    tvMoney.setText("+" + bean.getTradeAmt());
                }
                tvSuccessState.setVisibility(View.VISIBLE);
                tvSuccessState.setText(orderState);
                tvState.setVisibility(View.GONE);
                tvStateSub.setVisibility(View.GONE);
                break;

            case Constant.TRADE_FAIL :
                iv.setImageResource(R.drawable.icon_transaction_detail_fail);
                tvMoney.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(bean.getTradeAmt())) {
                    llAmountPay.setVisibility(View.VISIBLE);
                    tvAmountPay.setText(bean.getTradeAmt());
                } else {
                    llAmountPay.setVisibility(View.GONE);
                }
                tvSuccessState.setVisibility(View.GONE);
                tvState.setVisibility(View.VISIBLE);
                tvState.setText(orderState);
                tvStateSub.setVisibility(View.VISIBLE);
                tvStateSub.setText(bean.getFailResaion());
                break;

            case Constant.TRADE_UNDO :
                iv.setImageResource(R.drawable.icon_transaction_detail_success_gray);
                tvMoney.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(bean.getTradeAmt())) {
                    llAmountPay.setVisibility(View.VISIBLE);
                    tvAmountPay.setText(bean.getTradeAmt());
                } else {
                    llAmountPay.setVisibility(View.GONE);
                }
                tvSuccessState.setVisibility(View.GONE);
                tvState.setVisibility(View.VISIBLE);
                tvState.setText(getApplicationContext().getResources().getString(R.string.trade_success));
                tvStateSub.setVisibility(View.VISIBLE);
                tvStateSub.setText(getApplicationContext().getResources().getString(R.string.trade_undo));
                break;

            case Constant.TRADE_IMPACT :
                iv.setImageResource(R.drawable.icon_transaction_detail_fail);
                tvMoney.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(bean.getTradeAmt())) {
                    llAmountPay.setVisibility(View.VISIBLE);
                    tvAmountPay.setText(bean.getTradeAmt());
                } else {
                    llAmountPay.setVisibility(View.GONE);
                }
                tvSuccessState.setVisibility(View.GONE);
                tvState.setVisibility(View.VISIBLE);
                tvState.setText(getApplicationContext().getResources().getString(R.string.trade_fail));
                tvStateSub.setVisibility(View.VISIBLE);
                tvStateSub.setText(getApplicationContext().getResources().getString(R.string.result_for_bank_callback));
            break;

            case Constant.TRADE_IMPACT_FAIL :
                iv.setImageResource(R.drawable.icon_transaction_detail_exception);
                tvMoney.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(bean.getTradeAmt())) {
                    llAmountPay.setVisibility(View.VISIBLE);
                    tvAmountPay.setText(bean.getTradeAmt());
                } else {
                    llAmountPay.setVisibility(View.GONE);
                }
                tvSuccessState.setVisibility(View.GONE);
                tvState.setVisibility(View.VISIBLE);
                tvState.setText("交易超时");
                tvStateSub.setVisibility(View.VISIBLE);
                tvStateSub.setText(getApplicationContext().getResources().getString(R.string.result_for_bank_callback));
                break;

            case Constant.TRADE_UNDO_SUCCESS :
                iv.setImageResource(R.drawable.icon_transaction_detail_success_gray);
                tvMoney.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(bean.getTradeAmt())) {
                    llAmountPay.setVisibility(View.VISIBLE);
                    tvAmountPay.setText(bean.getTradeAmt());
                } else {
                    llAmountPay.setVisibility(View.GONE);
                }
                tvSuccessState.setVisibility(View.GONE);
                tvState.setVisibility(View.VISIBLE);
                tvState.setText(orderState);
                tvStateSub.setVisibility(View.GONE);
                break;

            case Constant.TRADE_UNDO_FAIL :
                iv.setImageResource(R.drawable.icon_transaction_detail_fail);
                tvMoney.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(bean.getTradeAmt())) {
                    llAmountPay.setVisibility(View.VISIBLE);
                    tvAmountPay.setText(bean.getTradeAmt());
                } else {
                    llAmountPay.setVisibility(View.GONE);
                }
                tvSuccessState.setVisibility(View.GONE);
                tvState.setVisibility(View.VISIBLE);
                tvState.setText(getApplicationContext().getResources().getString(R.string.trade_undo_fail));
                tvStateSub.setVisibility(View.GONE);
                break;

            default:
                iv.setVisibility(View.GONE);
                tvMoney.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(bean.getTradeAmt())) {
                    llAmountPay.setVisibility(View.VISIBLE);
                    tvAmountPay.setText(bean.getTradeAmt());
                } else {
                    llAmountPay.setVisibility(View.GONE);
                }
                tvSuccessState.setVisibility(View.GONE);
                tvState.setVisibility(View.GONE);
                tvStateSub.setVisibility(View.GONE);
                break;
        }
    }

    private void payTwoOrThree(String orderState, CashierDetailBean bean) {
        switch (bean.getTradeStatus()){
            case Constant.TRADE_SUCCESS :
                iv.setImageResource(R.drawable.icon_transaction_detail_success);
                tvMoney.setVisibility(View.VISIBLE);
                if (bean.getTradeAmt().startsWith("-") || bean.getTradeAmt().startsWith("+")) {
                    tvMoney.setText(bean.getTradeAmt());
                } else {
                    tvMoney.setText("+" + bean.getTradeAmt());
                }
                llAmountPay.setVisibility(View.GONE);
                tvSuccessState.setVisibility(View.VISIBLE);
                tvSuccessState.setText(orderState);
                tvState.setVisibility(View.GONE);
                tvStateSub.setVisibility(View.GONE);
                break;

            case Constant.TRADE_FAIL :
                iv.setImageResource(R.drawable.icon_transaction_detail_fail);
                tvMoney.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(bean.getTradeAmt())) {
                    llAmountPay.setVisibility(View.VISIBLE);
                    tvAmountPay.setText(bean.getTradeAmt());
                } else {
                    llAmountPay.setVisibility(View.GONE);
                }
                tvSuccessState.setVisibility(View.GONE);
                tvState.setVisibility(View.VISIBLE);
                tvState.setText(orderState);
                tvStateSub.setVisibility(View.VISIBLE);
                tvStateSub.setText(bean.getFailResaion());
                break;

            case Constant.TRADE_DEALING :
                iv.setImageResource(R.drawable.icon_transaction_detail_processing);
                tvMoney.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(bean.getTradeAmt())) {
                    llAmountPay.setVisibility(View.VISIBLE);
                    tvAmountPay.setText(bean.getTradeAmt());
                } else {
                    llAmountPay.setVisibility(View.GONE);
                }
                tvSuccessState.setVisibility(View.GONE);
                tvState.setVisibility(View.VISIBLE);
                tvState.setText(orderState);
                tvStateSub.setVisibility(View.GONE);
                break;

            default:
                iv.setVisibility(View.GONE);
                tvMoney.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(bean.getTradeAmt())) {
                    llAmountPay.setVisibility(View.VISIBLE);
                    tvAmountPay.setText(bean.getTradeAmt());
                } else {
                    llAmountPay.setVisibility(View.GONE);
                }
                tvSuccessState.setVisibility(View.GONE);
                tvState.setVisibility(View.GONE);
                tvStateSub.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public void onError() {

    }
}
